/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-26 11:16:06
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-01-26 17:39:57
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/device_detail/device/device_view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/model/device_model.dart';
import 'package:yt_devops/pages/devices/device_tab/components/detail_grid_widget.dart';
import 'package:yt_devops/pages/devices/device_tab/utils/device_util.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/utils/enums.dart';
import 'device_controller.dart';

class DevicePage extends GetView<DeviceController> {
  DeviceModel? deviceModel;
  int deviceId;
  DevicePage({Key? key, required this.deviceId, required this.deviceModel})
      : super(key: key);

  @override
  DeviceController controller = Get.put<DeviceController>(DeviceController());

  var actionKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DeviceController>(
      initState: (state) {
        controller.deviceId = deviceId;
        controller.deviceModel = deviceModel;
        if (deviceModel != null) {
          controller.init(deviceModel!);
        }
      },
      dispose: (state) {
        Get.delete<DeviceController>();
      },
      builder: (_) => Scaffold(
        // appBar: controller.actionList.isEmpty
        //     ? AppBarWidget.defaultAppBar(controller.deviceModel?.name ?? '')
        //     : AppBarWidget.withMorePopMenu(
        //         controller.deviceModel?.name ?? '',
        //         context,
        //         actionKey,
        //         data: controller.actionList,
        //         onItemClick: (int index, dynamic value) async {
        //           await controller.toService(value);
        //           return true;
        //         },
        //       ),
        body: StreamBuilder(
            stream: controller.streamController?.stream,
            builder: (context, snapshot) {
              var dataSource = snapshot.data ?? {};
              Log.d('dataSource==' + dataSource.toString());
              return SmartRefresher(
                controller: RefreshController(),
                enablePullDown: false,
                enablePullUp: false,
                child: controller.deviceModel == null
                    ? BrnAbnormalStateWidget(
                        img: Image.asset(
                          ImageAssets.networFail,
                          scale: 3.0,
                        ),
                        isCenterVertical: true,
                        title: "加载失败，请重试",
                        operateTexts: const ["请点击页面重试"],
                        operateAreaType: OperateAreaType.textButton,
                        action: (index) {
                          controller.getDeviceData();
                        },
                      )
                    : ListView.builder(
                        padding: EdgeInsets.zero,
                        itemBuilder: (context, index) =>
                            DeviceUtil.blockContainer(
                          Column(
                            children: [
                              DetailGridWidget(
                                isSingleRow: controller.isSingleLine,
                                title:
                                    controller.modelList[index].groupName ?? '',
                                items: controller.modelList[index].items,
                                dataSource: dataSource,
                                isLightTtext: controller.connectStatus !=
                                    ConnectStatusType.on,
                              ),
                            ],
                          ),
                        ),
                        itemCount: controller.modelList.length,
                      ),
              );
            }),
      ),
    );
  }
}
